/*
//
//              INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license  agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in  accordance  with the terms of that agreement.
//    Copyright (c) 2007-2008 Intel Corporation. All Rights Reserved.
//
//
*/

#include "umc_defs.h"
#if defined(UMC_ENABLE_AVS_VIDEO_DECODER) || \
    defined(UMC_ENABLE_AVS_VIDEO_ENCODER)

#include "umc_avs_thread.h"
#include "umc_avs_fusion_core.h"
#include "umc_avs_processing_unit.h"

#include "umc_event.h"
#include "vm_time.h"

namespace UMC
{

AVSThread::AVSThread(void)
{
    m_bQuit = false;
    m_bSleep = false;
    m_threadNum = 0;

} // AVSThread::AVSThread(void)

AVSThread::~AVSThread(void)
{
    // Release the object
    Close();

} // AVSThread::~AVSThread(void)

Status AVSThread::Close(void)
{
    // stop running thread
    if (IsValid())
    {
        m_bQuit = true;
        Wait();
    }

    // call the parent's method
    Thread::Close();

    m_bQuit = false;
    m_threadNum = 0;

    return UMC_OK;

} // Status AVSThread::Close(void)

Status AVSThread::Init(AVSFusionCore *pCore, Ipp32s iNumber)
{
    // check error(s)
    if (NULL == pCore)
        return UMC_ERR_NULL_PTR;

    // release object before initialization
    AVSThread::Close();

    // there is no parent's Init method, so we call nothing

    // do all custom initializations
    m_pCore = pCore;
    m_threadNum = iNumber;

    // run the thread
    if (m_threadNum)
    {
        if (UMC_OK != Thread::Create(ThreadProc, this))
            return UMC_ERR_FAILED;
    }

    return UMC_OK;

} // Status AVSThread::Init(AVSPictureBuffer *pPicBuffer, Ipp32s iNumber)

void AVSThread::GoSleep(Event &done)
{
    // set the sleep flag & sync object
    m_bSleep = true;
    m_pSignal = &done;

    // wait for the thread
    done.Wait();

} // void AVSThread::GoSleep(Event &done)

void AVSThread::WakeUp(Event &done)
{
    // set the sleep flag & sync object
    m_bSleep = false;
    m_pSignal = &done;

    // wait for the thread
    done.Wait();

} // void AVSThread::WakeUp(Event &done)

Status AVSThread::DoJob(void)
{
    while (false == m_bQuit)
    {
        AVSProcessingUnit *pPerformer;
        Status umcRes;

        // get the next task
        pPerformer = m_pCore->GetTask(m_threadNum);

        try
        {
            // do the job
            umcRes = pPerformer->DoJob();

        }
        catch(...)
        {
            // in case of error,
            // we need additional action
            umcRes = pPerformer->HandleError();

        }

        // there is a single quit condition for main thread.
        // all other threads quit by flag.
        if (UMC_OK == umcRes)
        {
            break;
        }

        // sleep handling.
        // this is a special case. We need to stop all the threads
        // to reset the decode/encoder.
        if (m_bSleep)
        {
            // let the main thread know I am sleeping
            m_pSignal->Set();

            // sleep while flag is set
            while (m_bSleep)
                vm_time_sleep(5);

            // let the main thread know I have woken up
            m_pSignal->Set();
        }
    }

    return UMC_OK;

} // Status AVSThread::DoJob(void)

Ipp32u AVSThread::ThreadProc(void *pParam)
{
    AVSThread *pOwner = (AVSThread *) pParam;

    // check error(s)
    if (NULL == pOwner)
        return 0x05decbad;

    // drop into the working cicle
    pOwner->DoJob();

    return 0x05dec000 + pOwner->m_threadNum;

} // Ipp32u AVSThread::ThreadProc(void *pParam)

} // namespace UMC

#endif // defined(UMC_ENABLE_AVS_VIDEO_DECODER)
